/*
 * proj02.cpp
 *
 *  Created on:  Jun 12, 2009
 *      Author:  Zach Riggle A37627537
 *      Project: CSE410 Project 2
 */

#include <unistd.h>
#include "CommandLineParser.h"
#include "NumberListFile.h"
#include "LockedNumber.h"
#include "OddEvenSorter.h"
#include "SortVerifier.h"

int main(int argc, char** argv) {
    NumberListFile nlf;
    CommandLineParser clp(argc, argv);
    if (!clp.isValid())
        return -1;

    // Load the data from input
    int loaded = nlf.load(clp.getInputFile());
    if (loaded < 1)
        warn("Did not load any numbers from the file " << clp.getInputFile());

    // Create the list
    SafeNumberList *snl = new SafeNumberList();
    snl->setList(nlf.getNumbers());

    // Create the sorter
    OddEvenSorter oes(snl);

    // Set the mode
    if (clp.isParallel()) {
        oes.setMode(OddEvenSorter::MULTI_PROCESS);
    } else if (clp.isThreaded()) {
        oes.setMode(OddEvenSorter::MULTI_THREAD);
    } else {
        oes.setMode(OddEvenSorter::SINGLE_PROCESS);
    }

    // Run
    clock_t begin = clock();
    oes.run();
    clock_t end = clock();
    cout << "Time Elapsed: " << (float)(end-begin)/CLOCKS_PER_SEC << " seconds." << endl;

    // Verify the sort
    SortVerifier::verifySort(snl->getList());

    // Grab the results
    nlf.setNumbers(snl->getList());

    // Save the data to output file
    int saved = nlf.save(clp.getOutputFile());
    if (saved < 1)
        warn("Did not save any numbers to the file " << clp.getOutputFile());

    return 0;
}
